#ifdef PULSECOUNTERS
#ifdef PULSECOUNTER1
#define PulsePin1                     2  // digital pin 2, input, interrupt driven
unsigned int PulseValue1=0;
unsigned long PulseCountPrev1=0;
volatile unsigned long PulseCount1=0;
volatile unsigned long PulseTime1=0;
volatile unsigned long PulseTimePrevious1=0;
volatile unsigned long DeltaPulseCount1=0;
#endif

#ifdef PULSECOUNTER2
#define PulsePin2                     3  // digital pin 3, input, interrupt driven
unsigned int PulseValue2=0;
unsigned long PulseCountPrev2=0;
volatile unsigned long PulseCount2=0;
volatile unsigned long PulseTime2=0;
volatile unsigned long PulseTimePrevious2=0;
volatile unsigned long DeltaPulseCount2=0;
#endif

#ifdef PULSECOUNTER3
unsigned int PulseValue3=0;
unsigned long PulseCountPrev3=0;
unsigned long PulseCount3=0;
unsigned long PulseTime3=0;
unsigned long PulseTimePrevious3=0;
unsigned long DeltaPulseCount3=0;
#endif

/*********************************************************************************************\ 
 * Dit deel van de plugin wordt eenmalig aangeroepen direct na boot en tonen welkomsttekst
 \*********************************************************************************************/
void UserPlugin_PC_Init(void)
{
#ifdef PULSECOUNTER1
  pinMode(PulsePin1, INPUT);
  digitalWrite(PulsePin1, HIGH);       // turn on pullup resistor
  attachInterrupt(0, interruptPulse1, FALLING);
  PulseTimePrevious1=millis();
#endif

#ifdef PULSECOUNTER2
  pinMode(PulsePin2, INPUT);
  digitalWrite(PulsePin2, HIGH);       // turn on pullup resistor
  attachInterrupt(1, interruptPulse2, FALLING);
  PulseTimePrevious2=millis();
#endif

}

void UserPlugin_PC_Command(int Par1, int Par2)
{
#ifdef PULSECOUNTER1
  if (Par1 == 146) {
    Serial.println(" RF Transmit port 1 Flow!");
    Nodo_2_RawSignal(AnalogInt2event(PulseValue1/10, Par2, CMD_VARIABLE_MEGA));
    RawSendRF();
  }
  if (Par1 == 147) {
    Serial.println(" RF Transmit port 1 Pulses!");
    Nodo_2_RawSignal(AnalogInt2event(DeltaPulseCount1/10, Par2, CMD_VARIABLE_MEGA));
    RawSendRF();
    DeltaPulseCount1=0;
  }
#endif

#ifdef PULSECOUNTER2
  if (Par1 == 148) {
    Serial.println(" RF Transmit port 2 Flow!");
    Nodo_2_RawSignal(AnalogInt2event(PulseValue2/10, Par2, CMD_VARIABLE_MEGA));
    RawSendRF();
  }

  if (Par1 == 149) {
    Serial.println(" RF Transmit port 3 Pulses!");
    Nodo_2_RawSignal(AnalogInt2event(DeltaPulseCount2/10, Par2, CMD_VARIABLE_MEGA));
    RawSendRF();
    DeltaPulseCount2=0;
  }
#endif

#ifdef PULSECOUNTER3
  if (Par1 == 150) {
    Serial.println(" RF Transmit port 3 Flow!");
    Nodo_2_RawSignal(AnalogInt2event(PulseValue3/10, Par2, CMD_VARIABLE_MEGA));
    RawSendRF();
  }
  if (Par1 == 151) {
    Serial.println(" RF Transmit port 3 Pulses!");
    Nodo_2_RawSignal(AnalogInt2event(DeltaPulseCount3/10, Par2, CMD_VARIABLE_MEGA));
    RawSendRF();
    DeltaPulseCount3=0;
  }
#endif

#ifdef PULSECOUNTERS
  if (Par1 == 152) userPluginPulseInfo();
#endif

}

void UserPlugin_PC_Periodically()
{

  CheckPulseCounters();

  return;
}

/*********************************************************************************************\
 * Puls counter stuff
 \*********************************************************************************************/
#ifdef PULSECOUNTER1
void interruptPulse1()
{
  if ((millis()-PulseTimePrevious1) > 100) {
    PulseTime1=millis()-PulseTimePrevious1;
    PulseTimePrevious1=millis();
    PulseCount1 = PulseCount1 + S.UserVar[2];  // var 3 holds increment for port 1
    DeltaPulseCount1 = DeltaPulseCount1 + S.UserVar[2];
  }
}
#endif

#ifdef PULSECOUNTER2
void interruptPulse2()
{
  if ((millis()-PulseTimePrevious2) > 100) {
    PulseTime2=millis()-PulseTimePrevious2;
    PulseTimePrevious2=millis();
    PulseCount2 = PulseCount2 + S.UserVar[5];  // var 6 holds increment for port 2
    DeltaPulseCount2 = DeltaPulseCount2 + S.UserVar[5];
  }
}
#endif

#ifdef PULSECOUNTER3
void analogPulse()
{
  PulseTime3=millis()-PulseTimePrevious3;
  PulseTimePrevious3=millis();
  PulseCount3 = PulseCount3 + S.UserVar[8];  // var 9 holds increment for port 3
  DeltaPulseCount3 = DeltaPulseCount3 + S.UserVar[8];
}
#endif

void CheckPulseCounters(void)
{

  if (PulseCount1 != PulseCountPrev1) {
    digitalWrite(MonitorLedPin, HIGH);
    delay(50);
    digitalWrite(MonitorLedPin, LOW);
    if ((PulseTime1 > 0) && (PulseCount1 > 1)) {
      PulseValue1 = (S.UserVar[0] * pow(10,S.UserVar[1])) / PulseTime1;
    }
    PulseCountPrev1 = PulseCount1;
  }

  if (PulseCount2 != PulseCountPrev2) {
    digitalWrite(MonitorLedPin, HIGH);
    delay(50);
    digitalWrite(MonitorLedPin, LOW);
    if ((PulseTime2 > 0) && (PulseCount2 > 1)) {
      PulseValue2 = (S.UserVar[3] * pow(10,S.UserVar[4])) / PulseTime2;
    }
    PulseCountPrev2 = PulseCount2;
  }

  if (PulseCount3 != PulseCountPrev3) {
    digitalWrite(MonitorLedPin, HIGH);
    delay(50);
    digitalWrite(MonitorLedPin, LOW);
    if ((PulseTime3 > 0) && (PulseCount3 > 1)) {
      PulseValue3 = (S.UserVar[6] * pow(10,S.UserVar[7])) / PulseTime3;
    }
    PulseCountPrev3 = PulseCount3;
  }

  if (millis()-PulseTimePrevious1 > 100000) {
    if (PulseValue1 > 0) {
      Serial.println(" Reset port 1 flow value!");
      PulseValue1=0;
    }
  }

  if (millis()-PulseTimePrevious2 > 100000) {
    if (PulseValue2 > 0) {
      Serial.println(" Reset port 2 flow value!");
      PulseValue2=0;
    }
  }

  if (millis()-PulseTimePrevious3 > 100000) {
    if (PulseValue3 > 0) {
      Serial.println(" Reset port 3 flow value!");
      PulseValue3=0;
    }
  }
}

void userPluginPulseInfo(void)
{
  Serial.print("PC1: ");  
  Serial.print(PulseCount1);
  Serial.print("  PC2:");  
  Serial.print(PulseCount2);
  Serial.print("  PC3:");  
  Serial.print(PulseCount3);
  Serial.print("  PT1:");  
  Serial.print(PulseTime1);
  Serial.print("  PT2:");  
  Serial.print(PulseTime2);
  Serial.print("  PT3:");  
  Serial.print(PulseTime3);
  Serial.print("  A0:");
  Serial.print(analogRead(14)/4);
  Serial.println();
}
#endif

